let gamerule = `
if (js.global.gameid == "") then
return
end
local window = js.global
local document = js.global.document
local module = "Public.GamesUI." .. window.gameid
local mjcc = require(module)
local table = document:getElementById("table")
local luaItem = {}
local initUserset = {}

function luaTransfrom(item)
return {
setEnabled = function(name, state)
item:setEnabled(name, state)
end,
setValue = function(name, state)
local htmlDom = document:getElementsByName(name)
local htmlValue = "0"
if (htmlDom.length > 1) then
    for m = 1, htmlDom.length do
        local htmlItem = htmlDom[m - 1]
        if (htmlItem:getAttribute('check') == 'checked') then
            htmlValue = htmlItem.value
        end
    end
else
    if (htmlDom[0]:getAttribute('check') == 'checked') then
        htmlValue = "1"
    else
        htmlValue = "0"
    end
end
if (state == htmlValue) then return end
item:setValue(name, state)
local temp = luaItem[name]
if (temp) then
    local func = temp.onvaluechanged
    if (func) then
        if (temp.type == "radiobutton") then
            local zu = document:getElementsByName(name)
            for m = 1, zu.length do
                local htmlItem = zu[m - 1]
                local itemTrans = luaTransfrom(htmlItem)

                if (htmlItem:getAttribute('check') == 'checked') then
                    local num = htmlItem.value
                    func(itemTrans, num)
                end
            end
        else
            local htmlItem = document:getElementsByName(name)[0]
            local num = changeEvent(htmlDom[0]:getAttribute('check') == 'checked')
            local itemTrans = luaTransfrom(htmlItem)
            func(itemTrans, num)
        end
    end
end
end
}
end

function changeEvent(condition)
local num = "0"
if (condition) then
num = "1"
else
num = "0"
end
return num
end

for i = 1, #mjcc.ui do
for j = 1, #mjcc.ui[i] do
local c = mjcc.ui[i][j]
local n = c.text ~= "局数:"
if(window.gameid == '200001') then
n = true
end
local m = c.valuename ~= "round"
if(window.gameid == '200001' and c.valuename == "round" and c.type == "radiobutton") then
local divSelect = document:createElement("button")
local wrap = document:createElement("label")
local button = document:createElement("input")
local desc = document:createElement("span")
local idval = string.gsub(c.exportvalue, "|", "")
desc.innerHTML = c.text
divSelect.innerHTML = c.text..'(<span>'..window.tuolaji[c.exportvalue]..'</span>张)'
divSelect.id = c.valuename .. idval
divSelect.classList:add('radio')
divSelect.classList:add(c.valuename)
divSelect:addEventListener('click',function()
local divsRadios = document:querySelectorAll('.'..divSelect:getAttribute('name'))
for a=1, divsRadios.length do
    divsRadios[a-1]:setAttribute('check', 'unchecked')
    divsRadios[a-1].classList:remove('select-item')
end
divSelect.classList:add('select-item')
divSelect:setAttribute('check', 'checked')
end)
desc:setAttribute("for", c.valuename .. c.exportvalue)
divSelect:setAttribute('type', 'radio')
divSelect:setAttribute('name', c.valuename)
divSelect:setAttribute('value', c.exportvalue)

button.type = "radio"
button.name = c.valuename
button.id = c.valuename .. idval
button.value = c.exportvalue

if (c.default == 1) then
button.check = "checked"
divSelect:setAttribute('check', 'checked')
divSelect.classList:add('select-item')
initUserset[c.valuename] = c.exportvalue
end
if (c.name ~= nil) then
button.className = c.name
divSelect.classList:add(c.name)
end
wrap.className = "rulewrap"
wrap:appendChild(button)
wrap:appendChild(desc)
wrap:appendChild(desc)
table:appendChild(divSelect)
end
if (c.type == "text" and n) then
local br = document:createElement('br')
local p = document:createElement("span")
p.classList:add('rule_item_each')
p.style.paddingRight = '14px'
p.innerHTML = c.text
if (c.text2) then
local span = document:createElement("span")
span.innerHTML = c.text2
span.style.paddingLeft = "20px"
span.style.color = "#1AAD19"
p:appendChild(span)
end
table:appendChild(p)
table:insertBefore(br, p)
elseif (c.type == "radiobutton" and c.valuename ~= "round") then
local divSelect = document:createElement("button")
local wrap = document:createElement("label")
local button = document:createElement("input")
local desc = document:createElement("span")
local idval = string.gsub(c.exportvalue, "|", "")
desc.innerHTML = c.text
divSelect.innerHTML = c.text
divSelect.id = c.valuename .. idval
divSelect.classList:add('radio')
divSelect.classList:add(c.valuename)
divSelect:addEventListener('click',function()
    local divsRadios = document:querySelectorAll('.'..divSelect:getAttribute('name'))
    for a=1, divsRadios.length do
        divsRadios[a-1]:setAttribute('check', 'unchecked')
        divsRadios[a-1].classList:remove('select-item')
    end
    divSelect.classList:add('select-item')
    divSelect:setAttribute('check', 'checked')
end)
desc:setAttribute("for", c.valuename .. c.exportvalue)
divSelect:setAttribute('type', 'radio')
divSelect:setAttribute('name', c.valuename)
divSelect:setAttribute('value', c.exportvalue)

button.type = "radio"
button.name = c.valuename
button.id = c.valuename .. idval
button.value = c.exportvalue

if (c.default == 1) then
    button.check = "checked"
    divSelect:setAttribute('check', 'checked')
    divSelect.classList:add('select-item')
    initUserset[c.valuename] = c.exportvalue
end
if (c.name ~= nil) then
    button.className = c.name
    divSelect.classList:add(c.name)
end
wrap.className = "rulewrap"
wrap:appendChild(button)
wrap:appendChild(desc)
wrap:appendChild(desc)
table:appendChild(divSelect)
elseif (c.type == "checkbox") then
local label = document:createElement("label")
local span = document:createElement("span")
local checkboxDiv = document:createElement('button')
local check = document:createElement("input")
local checkboxDivSet = luaTransfrom(checkboxDiv)
local checkTrans = luaTransfrom(check)
checkboxDiv.innerHTML = c.text
checkboxDiv.classList:add('somecheck')
checkboxDiv.classList:add('checkbox')
checkboxDiv.id = c.valuename
checkboxDiv.onclick = function()
    if(checkboxDiv:getAttribute('check') == 'checked') then
        checkboxDiv.classList:remove('select-item')
        checkboxDiv:setAttribute('check', 'unchecked')
        checkboxDiv:setAttribute('value', '0')
    else
        checkboxDiv.classList:add('select-item')
        checkboxDiv:setAttribute('check', 'checked')
        checkboxDiv:setAttribute('value', '1')
    end
    if (c.onvaluechanged) then
        local func = c.onvaluechanged
        local initNum = "0"
        local num = changeEvent(checkboxDiv:getAttribute('check') == 'checked')
        func(checkboxDivSet, num)
    end
end

checkboxDiv:setAttribute('name', c.valuename)
checkboxDiv:setAttribute('value', "0")


span.innerHTML = c.text
check.type = "checkbox"
check.name = c.valuename
check.value = "1"

if (c.name ~= nil) then
    check.className = c.name
    checkboxDiv.classList:add(c.name)
end
if (c.default == 1) then
    checkboxDiv.classList:add('select-item')
    checkboxDiv:setAttribute('check', 'checked')
    checkboxDiv:setAttribute('value', "1")
    check.check = "checked"
    initUserset[c.valuename] = "1"
else
    initUserset[c.valuename] = "0"
end
label.className = "rulewrap"
label:appendChild(check)
label:appendChild(span)
table:appendChild(checkboxDiv)
else
end
end
end

for i = 1, #mjcc.ui do
for j = 1, #mjcc.ui[i] do
local r = mjcc.ui[i][j]
if (r.type == "radiobutton") then
if (r.onvaluechanged) then
    luaItem[r.valuename] = r
    local zu = document:getElementsByName(r.valuename)
    local func = r.onvaluechanged
    for m = 1, zu.length do
        local item = zu[m - 1]
        local itemTrans = luaTransfrom(item)

        if (item:getAttribute('check') == 'checked') then
            local initNum = item:getAttribute('value')
            func(itemTrans, initNum)
        end

        item:addEventListener('click', function()
            local num = item.value
            func(itemTrans, num)
        end)
    end
end
elseif (r.type == "checkbox") then
if (r.onvaluechanged) then
    luaItem[r.valuename] = r
    local func = r.onvaluechanged
    local item = document:getElementsByName(r.valuename)[0]
    local itemTrans = luaTransfrom(item)
    local initNum = changeEvent(r.default == 1)
    func(itemTrans, initNum)
end
end
end
end

if (mjcc.getPlayerCount) then
local playerCount = mjcc.getPlayerCount(initUserset)
if (playerCount ~= nil) then
local waittime = document:querySelector(".rule_begin")
waittime:setAttribute('name','xianshi')
waittime.style.display = 'none'
end
end

function getRule()
local userset = window.userset
return mjcc.getRuler(userset)
end

function getPlayerCount()
local userset = window.userset
if (mjcc.getPlayerCount) then
local playerCount = mjcc.getPlayerCount(initUserset)
if (playerCount ~= nil) then
return mjcc.getPlayerCount(userset)
else
return false
end
else
return false
end
end
return mjcc
`;

export default gamerule;
